<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">编 号</div>
                      <el-input v-model="query.coding" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">名 称</div>
                      <el-input v-model="query.name" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="3" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_label">安排时间</div>
                      <el-date-picker v-model="query.arrange_date" type="datetimerange" placeholder="" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">资 产</div>
                      <v-select-s v-model="query.equipment_id" action="eqList" :query="{add_field: 'coding'}" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, applyOrderIndex) in cmStatus" :key="applyOrderIndex" :label="item" :value="applyOrderIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">类 别</div>
                      <el-select v-model="query.type" placeholder="请选择" size="mini">
                        <el-option v-for="(item, applyIndex) in applyType" :key="applyIndex" :label="item" :value="applyIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">故障模式</div>
                      <el-select v-model="query.mode" placeholder="请选择" size="mini">
                        <el-option v-for="(item, modeIndex) in applyMode" :key="modeIndex" :label="item" :value="modeIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">车间</div>
                      <el-select v-model="query.workshop" placeholder="请选择" size="mini">
                        <el-option key="0" label="冲压车间" value="0" />
                        <el-option key="1" label="车身车间" value="1" />
                        <el-option key="2" label="涂装车间" value="2" />
                        <el-option key="3" label="总装车间" value="3" />
                        <el-option key="4" label="质量部" value="4" />
                        <el-option key="5" label="生产管理部" value="5" />
                        <el-option key="6" label="设备动力部" value="6" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">来源类型</div>
                      <el-select v-model="query.cm_category" placeholder="请选择" size="mini">
                        <el-option key="0" label="扫码点检" value="0" />
                        <el-option key="1" label="一般点检" value="1" />
                        <el-option key="2" label="专业点检" value="2" />
                        <el-option key="3" label="自主维护" value="3" />
                        <el-option key="4" label="专业维护" value="4" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item>
                      <div class="search_label">提报人</div>
                      <v-select-s v-model="query.create_admin_id" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="2">
                    <el-form-item>
                      <div class="search_label">操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox" hidden>
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox" hidden>
                <el-button type="primary" icon="el-icon-tickets" size="mini" @click="exportHandler">导出</el-button>
              </div>
              <div class="buttonBox" hidden>
                <el-button type="primary" icon="el-icon-box" size="mini" :disabled="!selection.length" @click="archive">归档</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" :disabled="!selection.length" size="mini" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{ total }}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table v-loading="loading" :data="cmList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
        <el-table-column type="index" label="序号" align="center" width="50" />
        <el-table-column type="selection" align="center" width="45" />
        <el-table-column prop="coding" label="编号" align="center" width="100" />
        <el-table-column prop="name" label="名称" width="200" />
        <el-table-column prop="status" label="状态" align="center" width="100" :formatter="statusFormat" />
        <el-table-column prop="eq.name" label="资产" align="center" width="180" />
        <el-table-column prop="eq.workshop_name.name" label="车间" align="center" width="130" />
        <el-table-column prop="eq.line_name.name" label="生产线" align="center" width="130" />
        <el-table-column prop="mode" label="故障模式" align="center" width="150" :formatter="modeFormat" />
        <el-table-column prop="cm_category" label="来源类型" align="center" :formatter="categoryFormat" />
        <el-table-column label="提报人" align="center" width="150">
          <template slot-scope="{row}">
            <span v-if="row.cr_admin">{{ row.cr_admin.name }}</span>
            <span v-else>111</span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="申请时间" align="center" width="150" />
        <el-table-column prop="carryout_date" label="开始执行时间" align="center" width="150" />
        <el-table-column v-if="listHandle" fixed="right" label="操作" width="200" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import API from '@/api/maintain/cm'
import { cmStatus, applyType, priority, influence, applyMode } from '@/utils/options'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
import xlsx from 'xlsx'
import exportMethod from '@/mixins/export'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getModeFormat', 'getCmFormat', 'archives'],
  components: {
    VSelect,
    VSelectS
  },
  mixins: [exportMethod],
  data() {
    return {
      loading: false,
      cmList: [],
      total: 0,
      query: { page: 1, limit: 15, is_file: 0 },
      selection: [],
      cmStatus,
      applyType,
      priority,
      influence,
      applyMode
    }
  },
  computed: {
    tableMaxHeight() {
      return window.innerHeight - 260 + 'px'
    },
    listHandle() {
      return this.$store.state.settings.listHandle
    }
  },
  watch: {},
  created() {
    const query = JSON.parse(sessionStorage.getItem('query'))
    if (query) {
      this.query = query
      sessionStorage.removeItem('query')
    }
    this.index()
  },
  methods: {
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    modeFormat(row) {
      return this.getModeFormat(row)
    },
    categoryFormat(row) {
      if (row.cm_category === 1) {
        return '一般点检'
      } else if (row.cm_category === 2) {
        return '专业点检'
      } else if (row.cm_category === 3) {
        return '自主维护'
      } else if (row.cm_category === 4) {
        return '专业维护'
      } else {
        return '扫码报修'
      }
    },
    index() {
      this.loading = true
      API.cmList(this.query).then(response => {
        this.total = response.data.total
        this.cmList = response.data.data
        console.log(this.cmList)
      }).finally(() => {
        this.loading = false
      })
    },
    exportHandler() {
      const data = [
        [
          '编号',
          '名称',
          '资产',
          '状态',
          '发现失效时间',
          '类别',
          '优先程度',
          '失效影响',
          '故障模式',
          '错误信息',
          '排序'
        ]
      ]
      let status = ''
      let type = ''
      let priority = ''
      let influence = ''
      let mode = ''
      if (this.selection.length === 0) {
        this.$message.error('请选择要到导出的数据')
        return false
      } else {
        this.selection.forEach((item) => {
          if (item.status === 0) {
            status = '申请'
          } else if (item.status === 1) {
            status = '分配'
          } else if (item.status === 2) {
            status = '完毕'
          }

          if (item.type === 0) {
            type = '维修'
          } else {
            type = '检测'
          }

          if (item.priority === 0) {
            priority = '所有'
          } else if (item.status === 1) {
            priority = '48小时以内'
          } else if (item.status === 2) {
            priority = '48小时以外'
          } else if (item.status === 3) {
            priority = '立即'
          }

          if (item.influence === 0) {
            influence = '停产'
          } else if (item.influence === 1) {
            influence = '影响不大'
          }

          if (item.mode === 0) {
            mode = '一般硬件损坏'
          } else if (item.mode === 1) {
            mode = '电器损坏'
          }
          data.push([
            item.coding,
            item.name,
            item.equipments.name,
            status,
            item.lose_date,
            type,
            priority,
            influence,
            mode,
            item.error,
            item.sort
          ])
        })
      }
      const workbook = {
        SheetNames: ['projectApply'],
        Sheets: {
          'projectApply': xlsx.utils.aoa_to_sheet(data)
        }
      }
      this.exportExec(workbook, '维护申请数据报表.xlsx')
    },
    archive() {
      const selection = this.selection
      this.archives(API, selection)
    },
    remove() {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      this.delOne(API, id, false)
    },
    addHandler() {
      this.goUrl('/maintain/cm/cm_add')
    },
    updateHandler(id) {
      sessionStorage.setItem('query', JSON.stringify(this.query))
      const url = `/maintain/cm/cm_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getCmFormat(row)
    }
  }
}
</script>
